package dao;

import model.Module;
import utils.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;

/**
 * 模块dao
 */
public class ModuleDao {

    /**
     * 增
     * @return rows 影响行数
     */
    public int insert(Module module){

        Connection con=null;
        PreparedStatement pstm=null;
        String sql="insert into module_table values(null,?,?,?,?,?,?) ";

        int rows=0;
        try {
            con= JDBCTools.getCon();
            pstm=con.prepareStatement(sql);

            pstm.setInt(1,module.getMoTableId());
            pstm.setString(2,module.getMoName());
            pstm.setString(3,module.getMoSite());
            pstm.setString(4,module.getMmoWhether());
            pstm.setInt(5,module.getMoSort());
            pstm.setString(6,module.getMoRemark());

            rows=pstm.executeUpdate();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.close(con,pstm);
        }
        return rows;
    }


    /**
     * 删除
     * @return rows 影响行数
     */
    public int delete(int moId){
        Connection con = null;
        PreparedStatement pstm = null;

        String sql = "delete from module_table where mo_id = ?";
        int rows = 0;
        try {
            con = JDBCTools.getCon();
            pstm = con.prepareStatement(sql);

            pstm.setInt(1,moId);
            rows = pstm.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.close(con,pstm);
        }
        return rows;
    }


    /**
     * 修改
     * @return rows 影响行数
     */
    public int update(Module module){
        Connection con = null;
        PreparedStatement pstm = null;

        String sql = "update module_table set mo_table_id=?,mo_name=?,mo_site=?,mo_whether=?,mo_sort=?,mo_remark=? where mo_id = ?";
        int rows = 0;
        try {
            con = JDBCTools.getCon();
            pstm = con.prepareStatement(sql);

            pstm.setInt(1,module.getMoTableId());
            pstm.setString(2,module.getMoName());
            pstm.setString(3,module.getMoSite());
            pstm.setString(4,module.getMmoWhether());
            pstm.setInt(5,module.getMoSort());
            pstm.setString(6,module.getMoRemark());
            pstm.setInt(7,module.getMoId());

            rows = pstm.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.close(con,pstm);
        }
        return rows;
    }


    /**
     * 查询全部
     * @return
     */
    public List<Module> selectAll(Integer moId){
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql = "select * from module_table where mo_id=?";
        List<Module> moduleList = new ArrayList<>();
        try {
            con = JDBCTools.getCon();
            pstm = con.prepareStatement(sql);
            pstm.setInt(1,moId);
            rs = pstm.executeQuery();
            while (rs.next()){
                Module module=new Module();
                module.setMoId(rs.getInt(1));
                module.setMoTableId(rs.getInt(2));
                module.setMoName(rs.getString(3));
                module.setMoSite(rs.getString(4));
                module.setMmoWhether(rs.getString(5));
                module.setMoSort(rs.getInt(6));
                module.setMoRemark(rs.getString(7));

                moduleList.add(module);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.close(con,pstm,rs);
        }
        return moduleList;
    }


    /**
     *根据ID查询
     * @return
     */
    public Module selectId(Integer moId){
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;

        String sql = "select * from module_table where mo_id = ?";
        try {
            con = JDBCTools.getCon();
            pstm = con.prepareStatement(sql);
            pstm.setInt(1,moId);
            rs = pstm.executeQuery();
            while (rs.next()){

                Module module=new Module();
                module.setMoId(rs.getInt(1));
                module.setMoTableId(rs.getInt(2));
                module.setMoName(rs.getString(3));
                module.setMoSite(rs.getString(4));
                module.setMmoWhether(rs.getString(5));
                module.setMoSort(rs.getInt(6));
                module.setMoRemark(rs.getString(7));

                return module;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.close(con,pstm,rs);
        }
        return null;
    }

}
